<?php
//$Id: Action.php 522 2012-08-15 09:22:00Z tomqin@gmail.com $

/**
 * Action 类
 *
 * @copyright       Copyright (C) 2010-2012 ARESCMS Inc.
 * @author          TomQin <tomqin@gmail.com>
 * @license         http://www.arescms.cn/license/
 */

class Action{

    protected $action = '';
    protected $method = '';
    protected $params = null;
    protected $cityId = 0;
    protected $user = null;

    public function __construct(){
        $this->action = get_class($this);
        $this->cityId = Core::getApp()->getCityId();

        View::setTplRoot('apps.'.Core::getApp()->getAppName().'.template');
    }

    //@覆盖：初始化路由，决定 Method/Params 顺序（程序调用）
    public function setRoute($param1, $param2){
        $this->setMethod($param1);
        $this->setParams($param2);
    }

    //@覆盖：调用（程序调用）
    public function process(){
        $this->setVar('action', $this->action);

        $this->beforeAction();
        $this->callAction();
        $this->afterAction();
    }

    //@覆盖：开始 Action（程序调用）
    public function beforeAction(){}

    //@覆盖：执行 Action（程序调用）
    public function callAction(){
        if(method_exists($this, 'on'.$this->method)){
            $callAction = 'on'.$this->method;
        }elseif(method_exists($this, 'onDefault')){
            $callAction = 'onDefault';
        }else{
            exit('The method `' .$this->action.'::on'.$this->method. '` is not exists!');
        }
        $this->$callAction();
    }

    //@覆盖：结束 Action（程序调用）
    public function afterAction(){}

    public function setMethod($method){
        $this->method = empty($method) ? 'index' : $method;
    }

    public function setParams($params){
        $this->params = $params;
    }

    public function getMethod($method){
        return $this->method;
    }

    public function getParams($params){
        return $this->params;
    }

    protected function setVar($key, $var){
        View::setVar($key, $var);
    }

    protected function render($tpl){
        View::addView($tpl);
        View::display();
    }

    function seoContent(){
        $title = $keywords = $description = '';
        @list($title, $keywords, $description) = func_get_args();

        $cityname    = Core::getApp()->getCityName();
        $cityperfix  = Core::getApp()->getCityStr();
        $title       = str_replace('{CITY}', $cityname, $title.' - |速定客_{CITY}速订客，预订美好生活！');
        $keywords    = str_replace('{CITY}', $cityname, preg_replace('/[\|_ -,]/', ',', $title.$keywords));
        $description = str_replace('{CITY}', $cityname, isset($description) ? $description : $title);

        View::setVar('cityname', $cityname);
        View::setVar('cityperfix', $cityperfix);
        View::setVar('cityurl', $cityperfix.".sudingke.com");
        View::setVar('title', $title);
        View::setVar('keywords', $keywords);
        View::setVar('description', $description);
    }
}

?>